from Utils.RedisHelper import RedisHelper
from Utils.Function import *
from Spiders.XiciSpider import XiciSpider


if __name__ == '__main__' :

    redis = RedisHelper()
    keys = redis.getkeys()

    t = 0
    n = 0
    for key in keys:
        t = t + 1
        try:
            data = redis.get(key)
            proxy = {'http': 'http://{0}:{1}'.format(data['ip'], data['port'])}
            url = 'http://www.baidu.com/'
            r = requests.get(url, headers={'User-Agent': getua()}, timeout=10, proxies=proxy, verify=False)
            content = r.content
            content = content.decode('utf-8')
            if content.find('百度一下') > -1:
                n = n+1
                print(proxy)
        except Exception:
            print(Exception)

    print('{0} 个IP中，共有 {1} 个有效IP'.format(t, n))